<template>
    <div style="height: 95%;width: 100%;display:flex;justify-content:center;padding-top: 20px">
        <!--弹框DIV 设置弹框的大小的地方-->
        <div style="background: #fff;width: 700px;height: 600px;box-shadow: #D3DCE6 0px 0px 20px;">
            <div style="height:520px;border: #1C84C6;margin-top: 25px" class="scrollbar">
                <div style="height: 30px;">
                    <!--标题-->
                    <span style="font-size: 18px;margin-left: 25px;">
                        新建记录
                    </span>
                    <!--返回列表-->
                        <span style="margin-left: 540px;" @click="tzdz">
                            <i slot="suffix" style="font-size: 30px;color: #D3DCE6" class="el-icon-close"></i>
                        </span>
                </div>
                <!--height:91%;表单在弹框显示多长-->
                <el-scrollbar style="height:91%;padding-top: 20px;">
                    <div class="shugan"></div>
                    <span style="margin-left: 10px;font-size: 14px;">基本信息</span>
                    <br/>
                    <br/>
                    <!--表单-->
                    <el-form style="margin-top:5px;margin-left: 50px;" :rules="rules" size="mini" :model="fapiaolist" ref="fapiaolist">
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px;" prop="clientId">
                                    <label slot="label" style="font-size: 13px">客户：</label><br/>
                                    <!--客户名称-->
                                    <el-popover
                                            style="width: 700px; height: 450px;"
                                            placement="right"
                                            trigger="click"
                                            v-model="kehu">
                                        <span style="font-size: 16px;">关联客户模块</span>
                                        <br/>
                                        <el-input @keyup.enter.native="cx" v-model="kehuinput" style="width: 210px; margin-left: 230px; margin-bottom: 10px;">
                                            <i slot="suffix" class="el-input__icon el-icon-search" @click="cx"></i>
                                        </el-input>
                                        <el-button type="primary" style="margin-left: 130px;">新 建</el-button>
                                        <el-table
                                                highlight-current-row
                                                border
                                                ref="kehulist"
                                                :data="kehulist">
                                            <el-table-column type="selection" width="50" align="center" property="clientId"></el-table-column>
                                            <el-table-column width="150" property="clientName" label="客户名称"></el-table-column>
                                            <el-table-column width="100" property="clientRank" label="级别"></el-table-column>
                                            <el-table-column width="150" property="clientMobile" label="手机号码"></el-table-column>
                                            <el-table-column property="clientSite" label="地址"></el-table-column>
                                        </el-table>
                                        <el-pagination
                                                style="margin-left: 350px;"
                                                layout="prev, pager, next"
                                                :page-size="pageSize"
                                                :current-page.sync="currPage"
                                                @current-change="handleCurrentChange"
                                                :total="totalSize">
                                        </el-pagination>
                                        <div style="text-align: center;width: 100%" class="dialog-footer">
                                            <el-button type="primary" @click="xzkehu">保 存</el-button>
                                            <el-button type="primary" @click="kehu = false">取 消</el-button>
                                        </div>
                                        <el-input v-if="this.xg==0" v-model="fapiaolist.clientId.clientName" placeholder="+添加" slot="reference" :readonly='true' style="width: 250px;"></el-input>
                                    </el-popover>
                                    <el-input v-if="this.xg==1" :disabled="true" v-model="fapiaolist.clientId.clientName" placeholder="+添加" :readonly='true' style="width: 250px;"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px" prop="returnedmoneylogId">
                                    <label slot="label" style="font-size: 13px">回款记录：</label><br/>
                                    <!--回款计划-->
                                    <el-popover
                                            style="width: 700px; height: 450px;"
                                            placement="right"
                                            trigger="click"
                                            v-model="hueikuan">
                                        <span style="font-size: 16px;">关联记录模块</span>
                                        <br/>
                                        <el-input @keyup.enter.native="cx2" v-model="hkinput" style="width: 210px; margin-left: 230px; margin-bottom: 10px;">
                                            <i slot="suffix" class="el-input__icon el-icon-search" @click="cx2"></i>
                                        </el-input>
                                        <el-button type="primary" style="margin-left: 100px;">新 建</el-button>
                                        <el-table
                                                highlight-current-row
                                                border
                                                ref="hueikuanlist"
                                                :data="hueikuanlist">
                                            <el-table-column type="selection" width="50" align="center" property="returnedmoneylogId"></el-table-column>
                                            <el-table-column width="150" property="returnedmoneylogTheme" label="主题"></el-table-column>
                                            <el-table-column label="计划回款日期" align="center" width="200">
                                                <template slot-scope="d">
                                                    {{d.row.returnedmoneylogTime | formatDate}}
                                                </template>
                                            </el-table-column>
                                            <el-table-column width="100" property="returnedmoneylogIssue" label="期次"></el-table-column>
                                            <el-table-column width="100" property="returnedmoneylogMoney" label="金额"></el-table-column>
                                        </el-table>
                                        <el-pagination
                                                style="margin-left: 350px;"
                                                layout="total, prev, pager, next"
                                                @current-change="handleCurrentChange2"
                                                :page-size="pageSize2"
                                                :current-page.sync="currentpage2"
                                                :total="totalSize2">
                                        </el-pagination>
                                        <div style="text-align: center;width: 100%" class="dialog-footer">
                                            <el-button type="primary" @click="xzhueikuan">保 存</el-button>
                                            <el-button type="primary" @click="hueikuan = false">取 消</el-button>
                                        </div>
                                        <el-input v-if="this.xg==0" v-model="fapiaolist.returnedmoneylogId.returnedmoneylogTheme" @focus="dakai" placeholder="+添加" slot="reference" :readonly='true' style="width: 250px;"></el-input>
                                    </el-popover>
                                    <el-input v-if="this.xg==1" :disabled="true" v-model="fapiaolist.returnedmoneylogId.returnedmoneylogTheme" placeholder="+添加" :readonly='true' style="width: 250px;"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px" prop="invoiceNumber">
                                    <label slot="label" style="font-size: 13px">号码：</label><br/>
                                    <el-input v-model="fapiaolist.invoiceNumber"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px;" >
                                    <label slot="label" style="font-size: 13px">票据内容：</label><br/>
                                    <el-input v-model="fapiaolist.invoiceContent"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10">
                                <el-form-item prop="invoiceTime">
                                    <label slot="label" style="font-size: 13px">开票日期：</label><br/>
                                    <el-date-picker
                                            v-model="fapiaolist.invoiceTime"
                                            type="datetime"
                                            placeholder="选择日期时间"
                                            align="right"
                                            :picker-options="pickerOptions"
                                            style="width:250px">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px">
                                    <label slot="label" style="font-size: 13px">期次：</label><br/>
                                    <el-input v-model="fapiaolist.invoiceIssue" :disabled="true" style="width: 250px;"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10">
                                <el-form-item style="width:250px;" prop="invoiceType">
                                    <label slot="label" style="font-size: 13px">票据类型：</label><br/>
                                    <el-select v-model="fapiaolist.invoiceType" placeholder="请选择" style="width: 250px">
                                    <el-option
                                            v-for="item in options"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px"  prop="invoiceMoney">
                                    <label slot="label" style="font-size: 13px">票据金额：</label><br/>
                                    <el-input v-model="fapiaolist.invoiceMoney" :disabled="true"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="10">
                            </el-col>
                            <el-col :span="8">
                                <el-form-item style="width:250px;" prop="invoicePerson">
                                    <label slot="label" style="font-size: 13px">负责人：</label><br/>
                                    <el-input v-model="fapiaolist.invoicePerson" :disabled="true"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="18">
                                <el-form-item>
                                    <label slot="label" style="font-size: 13px">备注：</label><br/>
                                    <textarea style="width: 580px;" v-model="fapiaolist.invoiceRemark" placeholder="备注说明"></textarea>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <br/>
                    <br/>
                </el-scrollbar>
                <!--确定按钮 设置了固定定位-->
                <div style="text-align: center;width: 80%;position: absolute;top:635px;left:230px;" class="dialog-footer">
                     <el-button @click="tzdz">取 消</el-button>
                    &nbsp;&nbsp;
                    <el-button type="primary" @click="addbusiness('fapiaolist')">确 定</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import {formatTimeToStr} from '../router//format.js'
    export default {
        name: "ZxinzenFapiao",
        data() {
            var hueikuan = (rule, value, callback) => {
                if (this.fapiaolist.clientId.clientName === null || this.fapiaolist.clientId.clientName === '') {
                    callback(new Error('请先选择客户'));
                } else {
                    if (this.fapiaolist.returnedmoneylogId.returnedmoneylogTheme === '' || this.fapiaolist.returnedmoneylogId.returnedmoneylogTheme === null) {
                        callback(new Error('请选择 计划'));
                    }else {
                        callback()
                    }
                }
            };
            var kehua = (rule, value, callback) => {
                if (value.clientName === null || value.clientName === '') {
                    callback(new Error('请选择客户'));
                }else {
                    callback()
                }
            };
            var jine = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入金额'));
                } else {
                    var numReg = /^[0-9]*$/
                    var numRe = new RegExp(numReg)
                    if (!numRe.test(value)) {
                        callback(new Error('金额只能为数字'));
                    }else {
                        if (value <=0) {
                            callback(new Error('金额不能小于0'));
                        }else {
                            callback()
                        }
                    }
                }
            };
            return {
                rules: {
                    clientId: [
                        {validator: kehua, required: true, trigger: ['blur','change']},
                    ],
                    invoicePerson: [
                        { required: true, message: '请选择负责人' , trigger: 'change'},
                    ],
                    returnedmoneylogId: [
                        {validator: hueikuan, required: true, trigger: ['blur','change']},
                    ],
                    invoiceNumber: [
                        { required: true, message: '请输入发票号码' , trigger: 'change'},
                    ],
                    invoiceTime: [
                        {required: true, message: '请选择开票时间日期', trigger: 'change' },
                    ],
                    invoiceType: [
                        { required: true, message: '请选择发票类型'},
                    ],
                    invoiceMoney: [
                        {validator: jine, required: true, trigger: ['blur','change']},
                    ]
                },
                options: [{//客户场景选框信息
                    value: '',
                    label: ''
                }, {
                    value: '增值税',
                    label: '增值税'
                }, {
                    value: '普通国税',
                    label: '普通国税'
                }, {
                    value: '普通地税',
                    label: '普通地税'
                }, {
                    value: '收据',
                    label: '收据'
                }],
                pickerOptions: {//开票日期快捷选择方式
                    shortcuts: [{
                        text: '今天',
                        onClick(picker) {
                            picker.$emit('pick', new Date());
                        }
                    }, {
                        text: '昨天',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24);
                            picker.$emit('pick', date);
                        }
                    }, {
                        text: '一周前',
                        onClick(picker) {
                            const date = new Date();
                            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', date);
                        }
                    }]
                },
                kehu: false,//客户弹出框
                hueikuan: false,//回款弹出框
                kehuinput: '',//客户弹框搜索框
                hkinput: '',//回款弹框搜索框
                hueikuanlist: [],//回款表格信息
                kehulist: [],//客户表格信息
                fapiaolist: {
                    invoiceId: 0,
                    returnedmoneylogId: {
                        returnedmoneylogId: 0,
                        returnedmoneyId: '',
                        clientId: '',
                        returnedmoneylogTheme: '',
                        returnedmoneylogMoney: '',
                        returnedmoneylogTime: '',
                        returnedmoneylogIssue: '',
                        returnedmoneylogOwner: '',
                        returnedmoneylogPayment: '',
                        returnedmoneylogAccessory: '',
                        returnedmoneylogRefund: '',
                        returnedmoneylogBuck: '',
                        returnedmoneylogRate: '',
                        returnedmoneylogRemark: '',
                        returnedmoneylogZt: '',
                        returnedmoneylogInvoice: ''
                    },
                    clientId: {
                        clientId: 0,
                        updateTime: '',
                        clientIndustry: '',
                        clientSource: '',
                        clientUrl: '',
                        establishPerson: '',
                        establishTime: '',
                        clientName: '',
                        clientRank: '',
                        clientPhone: '',
                        clientMobile: '',
                        clientSite: '',
                        clientRemark: '',
                        clientPeople: '',
                        clientCjpeople: '',
                        clientState: 0,
                        tfyclueFormsByClueId: '',
                        tfylinkmanFormsByClientId: '',
                        zbartersByClientId: '',
                        zcontractsByClientId: '',
                        zdeliverylogsByClientId: '',
                        zinvoicesByClientId: '',
                        zordersByClientId: ''
                    },
                    invoiceContent: '',
                    invoiceType: '',
                    invoiceMoney: 0,
                    invoiceNumber: '',
                    invoiceTime: '',
                    invoiceIssue: '',
                    invoicePerson: '',
                    invoiceRemark: '',
                    invoiceZt: 0
                },//表单
                pageSize: 4,//客户分页页大小
                currPage: 1,//客户当前页码，默认为1
                totalSize: 0,//客户总记录数
                pageSize2: 4,//回款分页页大小
                currentpage2: 1,//回款当前页码，默认为1
                totalSize2: 0,//回款总记录数
                hkkehu: 0,//回款页面绑定客户查询
                xg: 0,//判断是新增还是修改
                fzr:[],
                ys:1,
                ydx:10,
                dz:''
            }
        },
        methods: {
            //地址
            tzdz(){
                if(this.dz==='htzy'){
                    this.$router.push({
                        path:'/heton',
                        query: {ys:this.ys,ydx:this.ydx}
                    });
                }else {
                    this.$router.push({
                        path:'/fapiao',
                        query: {ys:this.ys,ydx:this.ydx}
                    });
                }
            },
            addbusiness(formName) {
                if(this.xg!=0){
                    this.xzfapiao();
                }else{
                    this.$refs[formName].validate(valid => {
                        this.print(valid);
                        window.console.log(valid)
                        if (valid) {
                            this.xzfapiao();
                        }else{
                            this.$message({
                                message: '验证不通过！',
                                type: 'warning'
                            });
                            return false;
                        }

                    });
                }
            },
            //负责人查询方法
            yginitData(){
                this.$axios.post("http://localhost:8088/yg-list").then(v=>{
                        this.fzr = v.data;
                    })
                    .catch()
            },
            //点击回款计划输入框就查询回款计划
            dakai(){
                this.hkinitData(this.currentpage2);
            },
            //新增发票记录
            xzfapiao(){
                this.$axios.post("http://localhost:8088/vue/add_fapiao",this.fapiaolist)
                    .then(()=>{
                        if(this.xg==1){
                            this.$message({
                                showClose: true,
                                type: 'success',
                                message: '修改成功!',
                            });
                        }else{
                            this.$message({
                                showClose: true,
                                type: 'success',
                                message: '新增成功!',
                            });
                        }
                        this.tzdz();
                    }).catch(()=>{
                        if(this.xg==1){
                            this.$message({
                                showClose: true,
                                message: '修改失败',
                                type: 'error'
                            });
                        }else{
                            this.$message({
                                showClose: true,
                                message: '新增失败',
                                type: 'error'
                            });
                        }
                    }
                )
            },
            //选择客户确定按钮
            xzkehu() {
                this.$refs.kehulist.selection.forEach((pro)=>{
                    this.fapiaolist.clientId=pro;
                });
                this.kehu=false;
                this.kehuinput='';
            },
            //选择回款记录确定按钮
            xzhueikuan() {
                this.$refs.hueikuanlist.selection.forEach((pro)=>{
                    this.fapiaolist.returnedmoneylogId=pro;
                    this.fapiaolist.invoiceMoney=pro.returnedmoneylogMoney;
                    this.fapiaolist.invoiceIssue=pro.returnedmoneylogIssue;
                });
                this.hueikuan=false;
                this.hkinput='';
            },
            //客户查询方法
            initData(currentpage,pageSize){
                this.$axios.post("http://localhost:8088/dindan/kehu_list", this.qs.stringify(
                    {search:this.kehuinput, page:currentpage, size:pageSize}))
                    .then(v=>{
                        this.kehulist = v.data.rows;
                        this.totalSize = v.data.total;
                    })
                    .catch()
            },
            //回款计划查询方法
            hkinitData(currentpage){
                this.hkkehu=this.fapiaolist.clientId.clientId;
                window.console.log(this.hkinput)
                this.$axios.get("http://localhost:8088/hkjl//hkjl_list2",
                    {params:{page:currentpage,size:this.pageSize2,search:this.hkinput,search2: this.hkkehu}})
                    .then(v=>{
                            this.hueikuanlist = v.data.rows;
                            this.totalSize2 = v.data.total;
                        }
                    ).catch()
            },
            /*当前页方法*/
            handleCurrentChange(val) {
                this.initData(val,this.pageSize);
            },
            /*当前页方法*/
            handleCurrentChange2(val) {
                this.hkinitData(val);
            },
            /*当前页方法*/
            cx() {
                this.initData(this.currentpage,this.pageSize);
            },
            /*当前页方法*/
            cx2() {
                this.hkinitData(this.currentpage2);
            },
        },
        created: function () {
            this.fapiaolist.invoicePerson= JSON.parse(localStorage.getItem("user")).empName;
            this.initData(this.currPage,this.pageSize);
            this.yginitData();
            if(this.$route.query.key!=null){
                this.fapiaolist=this.$route.query.key;
                this.xg = 1;
            }
            if(this.$route.query.ys!=null){
                this.ys=this.$route.query.ys;
            }
            if(this.$route.query.ydx!=null){
                this.ydx=this.$route.query.ydx;
            }
            if(this.$route.query.dz!=null){
                this.dz=this.$route.query.dz;
            }
        },
        filters: {
            formatDate: function (time) {
                if (time != null && time != "") {
                    var date = new Date(time);
                    return formatTimeToStr(date, "yyyy-MM-dd");
                } else {
                    return "";
                }
            }
        }
    }
</script>

<style scoped>
    .shugan{
        border-left-color: rgb(70, 205, 207);
        border-left-width: 1.8px;
        height: 10px;
        border-left-style: solid;
        display: inline;
        font-size: 13px;
        margin-left: 25px;
    }

    /deep/ .el-scrollbar__wrap {
        overflow: scroll;
        width: 120%;
        height: 100%;
    }

</style>